/*
 * @Author: wangqizhang
 * @Date: 2024-04-29 15:17:13
 * @LastEditors: wangqizhang
 * @LastEditTime: 2024-05-12 15:03:42
 * @FilePath: /suixinji-server/service/index.js
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
const runSqlTask = (task) => {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await task();
      resolve({
        status: true,

        result: res.dataValues ? res.dataValues : res,
      });
    } catch (error) {
      reject({
        status: false,
        result: error,
      });
    }
  });
};
//任务映射
const taskMap = {
  create: (model, params) => {
    return () => model.create(params);
  },
  update: (model, params) => {
    return () => model.update(params, { where: { id: params.id } });
  },
  delete: (model, params) => {
    return () => model.destroy({ where: { id: params.id } });
  },
  findOne: (model, params) => {
    return () => model.findOne({ where: params });
  },
  findAll: (model, params) => {
    return () => model.findAll({ where: params });
  },
  //分页查询
  pageQuery: (model, params) => {
    return () => model.findAndCountAll(params);
  },
};
//创建查询任务
const initSqlTask = (model, type, params) => {
  return taskMap[type](model, params);
};
module.exports = {
  runSqlTask,
  initSqlTask,
};
